function [class] = classifier(epoch, spindle, kcomplex)

%%%%%%%%%%%%%%%%%%
%     class 3    %
%%%%%%%%%%%%%%%%%%

if spindle && kcomplex
    class = 3; %stage 2
    return
end

%%%%%%%%%%%%%%%%%%
%     class 1    %
%%%%%%%%%%%%%%%%%%

% average of the epoch
eAvg = mean(epoch);       

if  10 < eAvg < 30
    class = 1; %awake
    return
end

%%%%%%%%%%%%%%%%%%
%     class 2    %
%%%%%%%%%%%%%%%%%%      

% average amplitude of signals in frq range 2-7Hz
frq27 = bpfil(epoch, 2, 7);
meanFrq27 = mean(frq27);

if 100 < meanFrq27 < 200  && meanFrq27 > eAvg
    class = 2; %stage 1
    return
end


%%%%%%%%%%%%%%%%%%
% classes 4 and 5%
%%%%%%%%%%%%%%%%%%
%apply bypass filter to cut-off frq > 2Hz
frq2hz = bpfil(epoch, 0, 2);

%average amplitude of signals in frq range < 2Hz
meanFrq2hz = mean (frq2hz);

%computing percentage of points with frq less than 2Hz in a given epoch
count = 0;
len = length(epoch);
step = 100;    %break down the epochs to find signal parts with frq ~ 2
for i=0:step:len
    frq2hz = bpfil( epoch(i : min(len, i+step-1) ) );
    if mean (frq2hz) > 75
        count = count + 1;
    end
end
percentile = count/(len/step);


if meanFrq2hz > 75
    if percentile > 0.5
        class = 5; %stage 4
        return
    end
    if 0.2 < percentile < 0.5
        class = 4; %stage 3
        return
    end
end